package io.renren.modules.mvs.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.mvs.entity.ViolationTypeEntity;
import io.renren.modules.mvs.service.ViolationTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static io.renren.common.utils.Constant.FINE_REGEX;
import static io.renren.common.utils.Constant.SORT_VALUE_REGEX;


/**
 * 违章类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2025-03-30 17:08:24
 */
@RestController
@RequestMapping("mvs/violationType")
public class ViolationTypeController {
    @Autowired
    private ViolationTypeService violationTypeService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("mvs:violationType:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = violationTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("mvs:violationType:info")
    public R info(@PathVariable("id") Integer id){
		ViolationTypeEntity violationType = violationTypeService.getById(id);

        return R.ok().put("violationType", violationType);
    }

    /**
     * 保存
     */
    @SysLog("新增违章类型")
    @PostMapping("/save")
    @RequiresPermissions("mvs:violationType:save")
    public R save(@RequestBody ViolationTypeEntity violationType){
        if (!checkViolationTypeInfo(violationType)){
            return R.error("违章类型信息格式错误");
        }

		violationTypeService.save(violationType);
        return R.ok();
    }

    private boolean checkViolationTypeInfo(ViolationTypeEntity violationType) {
        return violationType.getFineAmount().toString().matches(FINE_REGEX)
                && violationType.getSortValue().toString().matches(SORT_VALUE_REGEX);
    }

    /**
     * 修改
     */
    @SysLog("修改违章类型")
    @PutMapping("/update")
    @RequiresPermissions("mvs:violationType:update")
    public R update(@RequestBody ViolationTypeEntity violationType){
        if (!checkViolationTypeInfo(violationType)){
            return R.error("违章类型信息格式错误");
        }

		violationTypeService.updateById(violationType);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除违章类型")
    @DeleteMapping("/delete")
    @RequiresPermissions("mvs:violationType:delete")
    public R delete(@RequestBody Integer[] ids){
		violationTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取违章类型名称
     */
    @GetMapping("/list/violationTypeName")
    @RequiresPermissions("mvs:violationType:list")
    public R violationTypeNameList(){
        List<ViolationTypeEntity> list = violationTypeService.getViolationTypeNameList();
        return R.ok().put("violationTypeList", list);
    }

}
